﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace CustomControls
{
    /// <summary>
    /// This class is an extention to Expander Control. 
    /// This provides a feature for shouwing a popup with the content of the expander on MouseOver
    /// </summary>
    public class CustomExpander : Expander
    {
        #region // Dependency Properties
        public static readonly DependencyProperty OldContentProperty = DependencyProperty.Register("OldContent", typeof(object), typeof(CustomExpander));

        private object OldContent
        {
            get { return GetValue(OldContentProperty); }
            set { SetValue(OldContentProperty, value); }
        }

        public static readonly DependencyProperty PopUpPlacementModeProperty = DependencyProperty.Register("PopUpPlacementMode", typeof(PlacementMode), typeof(CustomExpander));

        public PlacementMode PopUpPlacementMode
        {
            get { return (PlacementMode)GetValue(PopUpPlacementModeProperty); }
            set { SetValue(PopUpPlacementModeProperty, value); }
        }

        public static readonly DependencyProperty StaysOpenProperty = DependencyProperty.Register("StaysOpen", typeof(bool), typeof(CustomExpander));

        public bool StaysOpen
        {
            get { return (bool)GetValue(StaysOpenProperty); }
            set { SetValue(StaysOpenProperty, value); }
        }

        public static readonly DependencyProperty AllowsTransparencyProperty = DependencyProperty.Register("AllowsTransparency", typeof(bool), typeof(CustomExpander));

        public bool AllowsTransparency
        {
            get { return (bool)GetValue(AllowsTransparencyProperty); }
            set { SetValue(AllowsTransparencyProperty, value); }
        }

        public static readonly DependencyProperty PopUpWidthProperty = DependencyProperty.Register("PopUpWidth", typeof(double), typeof(CustomExpander), new PropertyMetadata(200.0));

        public double PopUpWidth
        {
            get { return (double)GetValue(PopUpWidthProperty); }
            set { SetValue(PopUpWidthProperty, value); }
        }

        public static readonly DependencyProperty PopUpHeightProperty = DependencyProperty.Register("PopUpHeight", typeof(double), typeof(CustomExpander), new PropertyMetadata(200.0));

        public double PopUpHeight
        {
            get { return (double)GetValue(PopUpHeightProperty); }
            set { SetValue(PopUpHeightProperty, value); }
        } 
        #endregion

        #region // .ctor(s)
        static CustomExpander()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomExpander), new FrameworkPropertyMetadata(typeof(CustomExpander)));
        }
        public CustomExpander()
        {
        } 
        #endregion

        #region // Overridden methods
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            object tmpContent = this.Content;
            this.Content = null;

            Viewbox containerViewBox = new Viewbox();
            containerViewBox.Child = (UIElement)tmpContent;
            OldContent = containerViewBox;
        } 
        #endregion
    }
}
